package modeladesktop.gui;
import javax.swing.table.*;
import java.util.*;

public class ModeloVariables extends AbstractTableModel{

	private ArrayList <String> vars = new ArrayList <String> ();
	private String [] cabeceras = {"Nombre"};

	public ArrayList <String> getVars(){
		return vars;
	}

	@Override
	public int getRowCount(){
		return vars.size();
	}

	@Override
	public String getColumnName(int col){
		return cabeceras[col];
	}

	@Override
	public int getColumnCount(){
		return 1;
	}

	@Override
	public Object getValueAt(int row, int col){
		return vars.get(row);
	}

	public void addRow(){
		vars.add(null);
		int pos = vars.size();
		fireTableRowsInserted(pos, pos + 1);
	}

	public boolean hasRepetida(){
		for(String str : vars){
			int rep = 0;
			for(String str2 : vars)
				if(str.equals(str2))
					rep++;
			if(rep == 2)
				return true;
		}
		return false;
	}

	public boolean hasNull(){
		for(String str : vars)
			if(str == null)
				return true;
		return false;
	}

	public void removeRow(int row){
		vars.remove(row);
		int pos = vars.size();
		fireTableRowsDeleted(pos, pos + 1);
	}

	@Override
	public boolean isCellEditable(int row, int col){
		return true;
	}

	@Override
	public void setValueAt(Object value, int row, int col){
		if(value != null)
			vars.set(row, value.toString());
		else
			vars.set(row, null);
	}
}
